package com.jml.zlhq.app.ui;

import java.util.Map;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Window;

import cn.waps.AppConnect;

import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.AppManager;
import com.umeng.analytics.MobclickAgent;
import com.zkmm.appoffer.OfferListener;
import com.zkmm.appoffer.ZkmmAppOffer;

public abstract class BaseFragmentActivity extends FragmentActivity implements IBaseUI {
	protected AppContext appContext;
	protected Activity activity;
	protected boolean pageFresh = false;
	protected boolean pageEnd = false;
	private boolean canDistroy = false;
	protected int pageIndex;
	protected Map<String,String> urlParam;
	 //用于对Fragment进行管理 
    protected FragmentManager fragmentManager;  
    
    //安沃
    public ZkmmAppOffer AnWoAppOffer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 竖屏幕
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		fragmentManager = getSupportFragmentManager();
		appContext = getAppContext();
		activity = getTActivity();
		
		//安沃 积分墙
//		AnWoAppOffer = ZkmmAppOffer.getInstance(this,"3ff72c43e7d4426392b1fd561bff527d");
//		AnWoAppOffer.setOfferListener(new OfferListener() {
//			
//			@Override
//			public void onReceiveAd() {
//				// TODO Auto-generated method stub
//				Log.i("ZLHQ", "安沃初始化成功");
//			}
//			
//			@Override
//			public void onFailedToReceiveAd() {
//				// TODO Auto-generated method stub
//				Log.e("ZLHQ", "安沃初始化失败");
//			}
//		});
	}

	@Override
	protected void onDestroy() {	
//		销毁夹在进度条对话框，因为在跳出当前activity是如果不再这里调用销毁进度条，会导致对话框失去附属activity。
		if(AnWoAppOffer!=null){
			AnWoAppOffer.dismissProgressDialog();
		}
		//万普退出
		AppConnect.getInstance(this).close();
		if(canDistroy){
			super.onDestroy();
			AppManager.getAppManager().removeActivity(this);
		}
	}
	
	@Override
	public void finish() {
		super.finish();
		canDistroy = true;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		if(pageFresh){
			pageFresh = false;
			clearData();
			loadData(urlParam);
		}
	}
    
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

	@Override
	public AppContext getAppContext() {
		return (AppContext) getApplication();
	}
	
	@Override
	public void setFreshPage(boolean pageFresh){
		this.pageFresh = pageFresh;
	}
	
	public void setPageEnd(boolean pageEnd){
		this.pageEnd = pageEnd;
	}
	
	@Override
	public Activity getTActivity() {
		return this;
	}

	public boolean isCanDistroy() {
		return canDistroy;
	}
	
}
